<template>
  <div class="lyric">
    <div class="lrc" ref="lrc">
      <p v-for="(item,key,index) in lyricObj" :key="index" class="lrc-p" :class="{'active':currentTime>allKey[index] && currentTime<allKey[index+1]}">
        <!--        <span v-if="currentTime>allKey[index] && currentTime<allKey[index+1]">{{item}}</span>-->
        <span>{{item}}{{scrollLRC(index)}}</span>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    id: {
      type: String,
      default: '1'
    },
    currentTime: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      lyricObj: '',
      allKey: []
    }
  },
  methods: {
    getData () {
      this.axios({
        params: {
          id: this.id
        },
        url: 'lyric'
      }).then(res => {
        this.lyricObj = res.data.lrc.lyric
        this.getLyric(res.data.lrc.lyric)
      })
    },
    getLyric (values) {
      if (!values) return
      const lrc = {}
      const l = values.split('\n')
      // console.log(l)
      const reg = /\[\d*:\d*(\.|:)\d*]/g
      for (let i = 0; i < l.length; i++) {
        const timeArr = l[i].match(reg)
        if (!timeArr) continue
        const content = l[i].replace(timeArr, '')
        const t = timeArr[0]
        const min = parseInt(t.match(/\[\d*/i).toString().slice(1))
        const sec = parseInt(t.match(/\:\d*/i).toString().slice(1))
        const time = min * 60 + sec
        lrc[time] = content
      }
      this.lyricObj = lrc
      this.getAllKey(lrc)
    },
    getAllKey (lrcArr) {
      for (const key in lrcArr) {
        this.allKey.push(key)
      }
    },
    scrollLRC (index) {
      if (this.currentTime > this.allKey[index] && this.currentTime < this.allKey[index + 1]) {
        this.$refs.lrc.style.top = -(30 * (index - 2)) + 'px'
      }
    }
  },
  created () {
    this.getData()
  }
}
</script>

<style lang="less" scoped>
.lyric{
  height: 150px;
  overflow-y: scroll;
  position: relative;
margin: 0 auto;
  .lrc{
    position: absolute;
    top: 0;
    width: 100%;
  }
  .lrc-p{
    height: 30px;
    line-height: 30px;
  }
  .active{
    color: orange;
    font-size: 25px;
  }
}
::-webkit-scrollbar {
  width: 5px;
}

/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background-color: #eee;
}
</style>
